package com.panopset.centralen.grid;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

/**
 * Flat drawing surface.
 *
 * @author Karl Dinwiddie
 *
 */
public final class Flat {

    /**
     * Mouse pressed x location.
     */
    private int pressedX = -1;

    /**
     * Mouse pressed y location.
     */
    private int pressedY = -1;

    /**
     * Report mouse moved.
     *
     * @param x x coordinate.
     * @param y y coordinate.
     */
    public void mouseMoved(final int x, final int y) {
        // nothing to do here
    }

    /**
     * Report mouse dragged.
     *
     * @param x x coordinate.
     * @param y y coordinate.
     */
    public void mouseDragged(final int x, final int y) {
        ShapeFactory.setRectangleFromInts(drag, x, y, pressedX, pressedY);
    }

    /**
     * Report mouse clicked.
     *
     * @param x x coordinate.
     * @param y y coordinate.
     */
    public void mouseClicked(final int x, final int y) {
        // nothing to do here
    }

    /**
     * Report mouse pressed.
     *
     * @param x x coordinate.
     * @param y y coordinate.
     */
    public void mousePressed(final int x, final int y) {
        pressedX = x;
        pressedY = y;
    }

    /**
     * Report mouse released.
     *
     * @param x x coordinate.
     * @param y y coordinate.
     */
    public void mouseReleased(final int x, final int y) {
        Rectangle r = ShapeFactory.createRectangleFromPoints(new Point(x,
                y), new Point(pressedX, pressedY));
        addRectangle(r);
        getDrag().height = 0;
        getDrag().width = 0;
        getDrag().x = -1;
        getDrag().y = -1;
    }

    /**
     * @return Rectangles.
     */
    public List <Rectangle> getRects() {
        if (rects == null) {
            rects = new ArrayList < Rectangle >();
        }
        return rects;
    }

    /**
     * Store all rectangles.
     */
    private List < Rectangle > rects;

    /**
     * Add rectangle.
     * @param r Rectangle to add.
     */
    private void addRectangle(final Rectangle r) {
        getRects().add(r);
    }

    /**
     *
     * @return Last drag boundry.
     */
    public Rectangle getDrag() {
        return drag;
    }

    /**
     * Mouse drag rectangle.
     */
    private final Rectangle drag = new Rectangle(-1, -1, 0, 0);

}
